# coding=utf-8

from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
from common.UrlManager import UrlManager
from flask_caching import Cache
from flask_redis import FlaskRedis
# from flask_mongoengine import MongoEngine
import os


class Application(Flask) :
    def __init__(self, import_name, template_folder = None, root_path = None ):
        super( Application, self ).__init__( import_name, template_folder=template_folder, root_path=root_path, static_folder = None)
        self.config.from_pyfile("config/base_config.py")
        self.config.from_pyfile("config/project_config.py")
        if "ops_config" in os.environ:
            self.config.from_pyfile( "config/%s_config.py" % (os.environ["ops_config"]) )
        cache.init_app(self, self.config["CACHE"])
        redis.init_app(self)
        # mongo.init_app(self, self.config)
        db.init_app( self )


# 初始化redis
redis = FlaskRedis()
# 初始化缓存
cache = Cache()
# 初始化mongo，主要用于坐标运算
# mongo = PyMongo()
# mongo = MongoEngine()
# 初始化数据库
db = SQLAlchemy( )
# 初始化整个项目
app = Application( __name__, template_folder = os.getcwd() + "/web/templates/", root_path = os.getcwd() )
# app进行包装
manager = Manager( app )




# jinja模板引擎函数扩展
app.add_template_global(UrlManager.buildUrl, "buildUrl")
app.add_template_global(UrlManager.buildStaticUrl, "buildStaticUrl")
app.add_template_global(UrlManager.buildImageUrl, "buildImageUrl")

# app.add_template_global(contains, 'contains')

